﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using Styx.CommonBot;

namespace UltimatePvPSuite
{
    public static class InsigniaRemover
    {
        public static WoWCorpse Target = null;
        public static bool Pulse()
        {
            if (LootCorpse())
                return true;
            return false;
        }

        private static bool LootCorpse()
        {
            if (CheckForPlayerCorpse() != null && UltimatePvPSuite.EnableLootInsignia)
            {
                // Target Newbie
                Target = CheckForPlayerCorpse();
                WoWPoint moveTo = StyxWoW.Me.Location;
                moveTo = WoWMathHelper.CalculatePointFrom(StyxWoW.Me.Location, Target.Location, Target.InteractRange - 5f);
                if (Target != null)
                {
                    if (Target.Distance < Target.InteractRange - 6f)
                    {
                        BotPoi.Current = new BotPoi(Target, PoiType.Loot);
                        Logger.Write("Lootable Corpse Spotted!. Switching to loot " + ((WoWCorpse)Target).SafeName() + "!");
                        Target.Interact();
                        Blacklist.Add(Target.Guid, new TimeSpan(0, 0, 200));
                        return true;
                    }
                }
            }
            return false;
        }

        private static WoWCorpse CheckForPlayerCorpse()
        {
            using (StyxWoW.Memory.AcquireFrame())
            {
                return (from Unit in ObjectManager.GetObjectsOfType<WoWCorpse>(false)
                        orderby Unit.Distance
                        where Unit.Distance < Convert.ToInt32(UltimatePvPSuite.LootInsigniaRange)
                        where Unit.InLineOfSight
                        where Unit.Lootable
                        where !Unit.IsOnlyBones
                        //where Unit.Owner.Equals(StyxWoW.Me)
                        where !Blacklist.Contains(Unit.Guid)
                        select Unit).FirstOrDefault();
            }
        }
    }
}
